using UnityEngine;
using System.Collections;


public class profiler : MonoBehaviour
{
 
	public Rect startRect = new Rect( 10, 10, 400, 800 ); // The rect the window is initially displayed at.
	//public bool updateColor = true; // Do you want the color to change if the FPS gets low
	public bool allowDrag = true; // Do you want to allow the dragging of the FPS window
	public  float frequency = 0.5F; // The update frequency of the fps
	public int nbDecimal = 1; // How many decimal do you want to display

	
	//items show or not
	public bool showEngine = true;
	public bool showFPS = true;
	//public bool showCases = false;
	//end items show.
	
	
	/*//collision case show or not
	public bool showCase1 = true;
	public bool showCase2 = true;
	public bool showCase3 = true;
	public bool showCase4 = true;
	public bool showCase5 = true;
	//end collision case.*/
	
	public int itemSpacing = 20; //spacing between items.
	
	public int fontsize = 30;
	
	
	/*private GameObject coin1,coin2;
	private Vector3 coin1Angles = new Vector3(90,90,0);
	private Vector3 coin2Angles = new Vector3(70,90,0);
	private Vector3 coin1Position = new Vector3(0,0,0);
	private Vector3 coin2Position = new Vector3(0,35,0);
	*/
	
	
	private float accum   = 0f; // FPS accumulated over the interval
	private int   frames  = 0; // Frames drawn over the interval
	private Color color = Color.white; // The color of the GUI, depending of the FPS ( R < 10, Y < 30, G >= 30 )
	private string sFPS = ""; // The fps formatted into a string.
	private GUIStyle style; // The style the text will be displayed at, based en defaultSkin.label.
 	private int sideLeft = 10;
	private int sideTop = 20;
	private int items = 0;
	//private float caseItemsSpacingScale = 2.0F;
	//private string Case = ""; 	
	
	private string[] name = new string[25];
	private string[] time = new string[25];
	private string[] calls = new string[25];
	private int i = 0;
	
	/*void awak()
	{
		Application.targetFrameRate = 300;
	}*/
	
	
	void Start()
	{
	    StartCoroutine( FPS() );
	
		/*coin1 = GameObject.Find("coin1");
		coin2 = GameObject.Find("coin2");
		coin1.rigidbody.freezeRotation = true;
		coin2.rigidbody.freezeRotation = true;
		
		Initial();*/
	
	}
 
	void Update()
	{
	    accum += Time.timeScale/ Time.deltaTime;
	    ++frames;
		btSimulation simProfiler;
		simProfiler = GetComponent<btSimulation>();
		
		
		for(i=0;i<25;i++)
		{
			calls[i] = simProfiler.calls[i].ToString("00000");
			name[i] = simProfiler.s[i];
			time[i]  = simProfiler.time[i].ToString("0000.000");
		}
		
		//stepSimulation
		//calls = simProfiler.calls;
		//name = simProfiler.s;
		//time = simProfiler.time;
		
	}
 
	IEnumerator FPS()
	{
		// Infinite loop executed every "frenquency" secondes.
		while( true )
		{
			// Update the FPS
		    float fps = accum/frames;
		    sFPS = fps.ToString( "f" + Mathf.Clamp( nbDecimal, 0, 10 ) );
 
			//Update the color
			//color = (fps >= 30) ? Color.green : ((fps > 10) ? Color.red : Color.yellow);
 
	        //accum = 0.0F;
	        //frames = 0;
 
			yield return new WaitForSeconds( frequency );
		}
	}
 
	void OnGUI()
	{
		//Copy the default label skin, change the color and the alignement
		if( style == null ){
			style = new GUIStyle( GUI.skin.label );
			style.normal.textColor = Color.white;
			style.alignment = TextAnchor.UpperLeft;
		}
 
		style.fontSize = fontsize;
		
		if (GUI.skin.customStyles.Length > 0)
            GUI.skin.customStyles[0].stretchWidth = true;
		 if(GUI.skin.customStyles.Length > 0)
            GUI.skin.customStyles[0].stretchHeight = true;
		
		//GUI.color = updateColor ? color : Color.white;
		GUI.color = color;
		startRect = GUI.Window(0, startRect, DoMyWindow, "");
		
	}
 
	
	void DoMyWindow(int windowID)
	{
		// Make a background box
		GUI.Box(new Rect (0,0,startRect.width,startRect.height) , "Profiler");
		
		
		//Show informations
		items = 0;
		if (showEngine) {GUI.Label( new Rect(sideLeft, sideTop+items*itemSpacing , startRect.width, startRect.height), "Engine: "        + items, style ); items++;}
		if (showFPS)    {GUI.Label( new Rect(sideLeft, sideTop+items*itemSpacing , startRect.width, startRect.height), "FPS: "           + sFPS , style ); items++;}
		if (true)    {GUI.Label( new Rect(sideLeft, sideTop+items*itemSpacing , startRect.width, startRect.height), "  calls      time         function" , style ); items++;}
		//if (showCases)  {GUI.Label( new Rect(sideLeft, sideTop+items*itemSpacing , startRect.width, startRect.height), "CollisionCase: " + Case , style ); items++;}
		for (i=0;i<25;i++)
		{
			if (true)  {GUI.Label( new Rect(sideLeft, sideTop+items*itemSpacing , startRect.width, startRect.height), "( " + calls[i] + " )  " + time[i] + "  " + name[i] , style ); items++;}
		}
		//if (true)  {GUI.Label( new Rect(sideLeft, sideTop+items*itemSpacing , startRect.width, startRect.height), "( " + calls[1] + " ) " + time[1] + " " + name[1] , style ); items++;}
		//if (true)  {GUI.Label( new Rect(sideLeft, sideTop+items*itemSpacing , startRect.width, startRect.height), "time: " + time , style ); items++;}
		//end show informations.
		
		
		
		
		
		
		
		/*//Show testCase
		if (showCase1){
			//if(GUI.Button(new Rect(sideLeft,sideTop+items*itemSpacing*caseItemsSpacingScale , startRect.width*0.8F, startRect.height*0.08F), "Case1"))
			if(GUI.Button(new Rect(sideLeft,sideTop+items*itemSpacing , startRect.width*0.8F, startRect.height*0.08F), "Case1")) 
			{
				Case = "1";
				coin1Position = new Vector3(0,0,0);
				coin2Position = new Vector3(0,35,0);
				coin1Angles = new Vector3(90,90,0);
				coin2Angles = new Vector3(70,90,0);
				Initial();		
			}
			items++;
		}
		
		
		if (showCase2){
			//if(GUI.Button(new Rect(sideLeft,sideTop+items*itemSpacing*caseItemsSpacingScale, startRect.width*0.8F, startRect.height*0.08F), "Case2"))
			if(GUI.Button(new Rect(sideLeft,sideTop+items*itemSpacing+startRect.height*0.08F, startRect.width*0.8F, startRect.height*0.08F), "Case2"))
			{
				Case = "2";
				coin1Position = new Vector3(0,0,0);
				coin2Position = new Vector3(0,25,0);
				coin1Angles = new Vector3(0,0,0);
				coin2Angles = new Vector3(90,0,0);
				Initial();
			}
			items++;	
		}
		
		
		if (showCase3){
			if(GUI.Button(new Rect(sideLeft,sideTop+items*itemSpacing+startRect.height*0.08F*2, startRect.width*0.8F, startRect.height*0.08F), "Case3"))
			{
				Case = "3";
				coin1Position = new Vector3(0,0,0);
				coin2Position = new Vector3(0,35,0);
				coin1Angles = new Vector3(90,0,0);
				coin2Angles = new Vector3(90,45,0);
				Initial();
			}
			items++;	
		}
		
		
		if (showCase4){
			if(GUI.Button(new Rect(sideLeft,sideTop+items*itemSpacing+startRect.height*0.08F*3, startRect.width*0.8F, startRect.height*0.08F), "Case4"))
			{
				Case = "4";
				coin1Position = new Vector3(0,0,0);
				coin2Position = new Vector3(10,15,0);
				coin1Angles = new Vector3(0,0,0);
				coin2Angles = new Vector3(0,0,0);
				Initial();
			}
			items++;	
		}
		
		
		if (showCase5){
			if(GUI.Button(new Rect(sideLeft,sideTop+items*itemSpacing+startRect.height*0.08F*4, startRect.width*0.8F, startRect.height*0.08F), "Case5"))
			{
				Case = "5";
				coin1Position = new Vector3(0,0,0);
				coin2Position = new Vector3(0,35,0);
				coin1Angles = new Vector3(90,0,0);
				coin2Angles = new Vector3(90,90,0);
				Initial();
			}
			items++;	
		}*/
		// end show cases.
		
		
		if( allowDrag ) GUI.DragWindow(new Rect(0, 0, Screen.width, Screen.height));
	}
	
	
	
	/*void OnCollisionExit(Collision collision)
	{
		Initial();
		//coin1.transform.eulerAngles = coin1Angles;
		//coin2.transform.eulerAngles = coin2Angles;
	}*/
	
	
/*	public void Initial()
	{
		coin1.transform.position = coin1Position;
		coin2.transform.position = coin2Position;
		//coin1.rigidbody.velocity = new Vector3(0,0,0);
		coin2.rigidbody.velocity = new Vector3(0,0,0);
		coin1.transform.eulerAngles = coin1Angles;
		coin2.transform.eulerAngles = coin2Angles;
	}*/
}